clear
set more off

**** CREATING ELECTORAL CONSTITUENCIES ****
foreach n of numlist 35(2.5)55 {
	local DEM=`n'
	local IND=10
	local REP=100-`n'-`IND'
	clear
	set obs 5000  // district with 5,000 voters
	* generating PID *
	gen id=_n
	gen pid=id
	local n1=`n'*50
	local n2=`n1'+1
	local n3=`n1'+500
	local n4=`n3'+1
	recode pid 1/`n1'=-1  `n2'/`n3'=0 `n4'/5000=1
	label define a -1 "DEM" 0 "IND" 1 "REP"
	label values pid a
	compress
	save district-`REP'R.dta, replace  // so this dataset is the district with partisans 
}


***  NOW THE SIMULATION ***
local j=0

local N=10000  // number of elections

foreach n of numlist 35(2.5)55 {

	local DEM=`n'
	local IND=10
	local REP=100-`n'-`IND'

	foreach D of numlist -3(.25)0 {   // Dcand location varies from -1 to -3 in .1 increments
		local j=`j'+1
		clear
		foreach v in election districtdem demcandideo repwinsym repwinasym {
			gen `v'=.
		}
		save simresults_`j'.dta ,replace // dataset to collect results 
		
		forvalues i=1/`N' {
			clear
			use district-`REP'R.dta
			gen respideo=.
			gen demcandideo=`D'
			gen repcandideo=1.5
			gen demdist=.
			gen repdist=.
			gen logitsym=.
			gen logitasym=.
			// the next three lines assign ideology to partisans with the designated means/sds
			replace respideo=rnormal(-1.25,1.25) if pid==-1 
			replace respideo=rnormal( 0   ,1.25) if pid== 0 
			replace respideo=rnormal( 1.25,1.25) if pid== 1 
			replace repdist=abs(respideo-repcandideo)   // now we calculate distances
			replace demdist=abs(respideo-demcandideo) 
* the following command sets <a> to a value between -1/1.  The idea is that in any
* given election there are election-specific factors (a partisan tide, valence, etc.
* that affects all voters in the same way.  I only figured out that it was necessary
* after first doing the simulation without it and finding a variety of problems. 
			local a=runiform(-1,1)  
			* symmetrical *
			replace logitsym=  -2.0 - .50*repdist + .50*demdist + `a'  if pid==-1 
			replace logitsym=   0.0 - .50*repdist + .50*demdist + `a'  if pid== 0 
			replace logitsym=   2.0 - .50*repdist + .50*demdist + `a'  if pid== 1  
			* asymmetrical *
			replace logitasym=  -2.0 - .50*repdist               + `a'  if pid==-1  // dems only react to repcand
			replace logitasym=   0.0 - .50*repdist + .50*demdist + `a'  if pid== 0
			replace logitasym=   2.0               + .50*demdist + `a'  if pid== 1  // reps only react to demcand
			gen probsym= exp(logitsym)/(1+exp(logitsym))  // now let's convert logits to probabilities
			gen probasym=exp(logitasym)/(1+exp(logitasym))
			gen votesym= rbinomial(1,probsym)  // now let's vote based on probabilities
			gen voteasym=rbinomial(1,probasym)
			collapse (sum) vote* // computes the repvote in each election
			gen repwinsym=.
			replace repwinsym=1 if votesym>2500
			replace repwinsym=0 if votesym<2500
			gen repwinasym=.
			replace repwinasym=1 if voteasym>2500
			replace repwinasym=0 if voteasym<2500
			gen demcandideo=`D' 
			gen districtdem=`DEM'
			gen election=`i'
			append using simresults_`j'.dta
			save simresults_`j'.dta ,replace
		} // next election
	}  // next value of demcandideo
}	// next district
* collecting the results *
clear
forvalues i = 1/`j' {
	append using simresults_`i'.dta
	erase simresults_`i'.dta
}
save simresults.dta ,replace
